package com.gzw.shop.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cjj.MaterialRefreshLayout;
import com.google.gson.reflect.TypeToken;
import com.gzw.shop.Contans;
import com.gzw.shop.R;
import com.gzw.shop.activity.WareDetailActivity;
import com.gzw.shop.adapter.BaseAdapter;
import com.gzw.shop.adapter.DividerItemDecoration;
import com.gzw.shop.adapter.HWAdapter;
import com.gzw.shop.bean.Page;
import com.gzw.shop.bean.Wares;
import com.gzw.shop.http.OkHttpHelper;
import com.gzw.shop.utils.Pager;
import com.gzw.shop.widget.GToolbar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.Unbinder;

public class HotFragment extends BaseFragment {

    @BindView(R.id.toolbar)
    GToolbar mToolbar;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.refresh)
    MaterialRefreshLayout mRefreshLayout;
    Unbinder unbinder;

    private OkHttpHelper mHelper = OkHttpHelper.getInstance();

    private HWAdapter mAdapter;

    private List<Wares> mWares = new ArrayList<>();

    private int currPage = 1;
    private int pageSize = 10;
    private int totalPage = 0;

    private static final int STATE_NORMAL = 0;//正常
    private static final int STATE_REFRESH = 1;//刷新
    private static final int STATE_MORE = 2;//加载更多

    private int state = STATE_NORMAL;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_hot;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

        requestData();

    }

    private void requestData() {

        Pager pager = Pager.newBuilder()
                .setUrl(Contans.API.WARES_HOT)
                .setCanLoadMore(true)
                .setOnPagerListener(new Pager.OnPagerListener() {
                    @Override
                    public void load(final List datas, int totalPage, int totalCount) {

                        mAdapter = new HWAdapter(getActivity(), datas);
                        mRecyclerView.setAdapter(mAdapter);
                        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
                        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));

                        mAdapter.setListener(new BaseAdapter.OnItemClickListener() {
                            @Override
                            public void OnClick(View view, int position) {

                                Intent intent = new Intent(getActivity(), WareDetailActivity.class);
                                intent.putExtra(Contans.WARES, (Wares) datas.get(position));
                                startActivity(intent);
                            }
                        });

                    }

                    @Override
                    public void refresh(List datas, int totalPage, int totalCount) {

                        mAdapter.clearData();
                        mAdapter.addData(datas);
                        mRecyclerView.scrollToPosition(0);
                        mRefreshLayout.finishRefresh();
                    }

                    @Override
                    public void loadMore(List datas, int totalPage, int totalCount) {

                        mAdapter.addData(mAdapter.getDatas().size(), datas);
                        mRecyclerView.scrollToPosition(mAdapter.getDatas().size());
                        mRefreshLayout.finishRefreshLoadMore();

                    }
                }).setPageSize(20)
                .setRefreshLayout(mRefreshLayout)
                .builder(getActivity(), new TypeToken<Page<Wares>>() {
                }.getType());
        pager.request();
    }

    public void showData() {
        if (mAdapter == null) {
            requestData();
        }
    }
}
